<template>
	<view>
		<view
			id="_drag_button"
			class="drag"
			:style="'left: ' + left + 'px; top:' + top + 'px;'"
			@touchstart="touchstart"
			@touchmove.stop.prevent="touchmove"
			@touchend="touchend"
			
			:class="{transition: isDock && !isMove }"
		>
			<view class="d-kuan d-neirong-juzhong">
				<view class="d-shang-10 d-zuo-10" style="margin-top: -30px;margin-left: 30px;" @click="kaiguan()">
					<u-icon name="close-circle" color="#ff0000"></u-icon>
				</view>
				<view class="d-kuan d-shangxian-juzhong d-shang-25" @click="gnkaiguan(1,fenggeyanse)">
					<u--image :src="bianlian" mode="widthFix" width="50" height="50"></u--image>
				</view>
				<view class="d-kuan d-shangxian-juzhong d-shang-10" @click="gnkaiguan(2,fenggeyanse)">
					<u--image :src="liebiao" mode="widthFix" width="50" height="50"></u--image>
				</view>
				<view class="d-kuan d-shangxian-juzhong d-shang-10" @click="gnkaiguan(3,fenggeyanse)">
					<u--image :src="anhei" mode="widthFix" width="50" height="50"></u--image>
				</view>
				<!--view class="d-kuan d-shangxian-juzhong d-shang-10">
					<u--image :src="bianlian" mode="widthFix" width="50" height="50"></u--image>
				</view>
				<view class="d-kuan d-shangxian-juzhong d-shang-10">
					<u--image :src="bianlian" mode="widthFix" width="50" height="50"></u--image>
				</view-->
			</view>
			
		</view>

	</view>
</template>

<script>
	import config from "../../utils/config";
	/*
	<DdragButton
		:isDock="true"
		:existTabBar="true"
		@btnClick="btnClick"
		@btnTouchstart="btnTouchstart"
		@btnTouchend="btnTouchend" />
		属性名	类型	默认值	说明
		isDock	Boolean	false	是否启用自动停靠
		existTabBar	Boolean	false	当前页面是否包含tabbar
		@btnClick	EventHandle		点击时会触发
		@btnTouchstart	EventHandle		拖拽开始时会触发
		@btnTouchend	EventHandle		拖拽结束时会触发
		@click.stop.prevent="click"
	*/
	export default {
		name: 'd-drag-button-s',
		props: {
			isDock:{
				type: Boolean,
				default: false
			},
			existTabBar:{
				type: Boolean,
				default: false
			},
			fenggeyanse:{
				type: [Number, String],
				default:0
			}
		},
		data() {
			return {
				top:420,
				left:40,
				
				/*width: 0,
				height: 0,
				offsetWidth: 0,
				offsetHeight: 0,
				windowWidth: 0,
				windowHeight: 0,
				isMove: true,
				edge: 40,*/
				width: '',
				height: '',
				offsetWidth: '',
				offsetHeight: '',
				windowWidth: '',
				windowHeight: '',
				isMove: true,
				edge: 40,
				text: '按钮',
				bianlian:config.getDomain+'/static/default/web/img/bianlian.png',
				liebiao:config.getDomain+'/static/default/web/img/liebiao.png',
				anhei:config.getDomain+'/static/default/web/img/anhei.png',
				bianliankaiguan:false
			}
		},
		mounted() {
			const sys = uni.getSystemInfoSync();
			
			this.windowWidth = sys.windowWidth;
			this.windowHeight = sys.windowHeight;
			
			// #ifdef APP-PLUS
				this.existTabBar && (this.windowHeight -= 50);
			// #endif
			if (sys.windowTop) {
				this.windowHeight += sys.windowTop;
			}
			console.log(sys)
			
			const query = uni.createSelectorQuery().in(this);
			query.select('#_drag_button').boundingClientRect(data => {
				this.width = data.width;
				this.height = data.height;
				this.offsetWidth = data.width / 2;
				this.offsetHeight = data.height / 2;
				//this.left = this.windowWidth - this.width - this.edge;
				this.left = 40;
				this.top = this.windowHeight - this.height - this.edge;
				this.top = 420
			}).exec();
		},
		onShow(){
			//this.dianji();
		},
		methods: {
			dianji(){
				const sys = uni.getSystemInfoSync();
				
				this.windowWidth = sys.windowWidth;
				this.windowHeight = sys.windowHeight;
				
				// #ifdef APP-PLUS
					this.existTabBar && (this.windowHeight -= 50);
				// #endif
				if (sys.windowTop) {
					this.windowHeight += sys.windowTop;
				}
				console.log(sys)
				
				const query = uni.createSelectorQuery().in(this);
				query.select('#_drag_button').boundingClientRect(data => {
					this.width = data.width;
					this.height = data.height;
					this.offsetWidth = data.width / 2;
					this.offsetHeight = data.height / 2;
					this.left = this.windowWidth - this.width - this.edge;
					//this.left = 40;
					this.top = this.windowHeight - this.height - this.edge;
				}).exec();
			},
			click() {
				this.dianji();
				this.$emit('btnClick');
			},
			touchstart(e) {
				this.$emit('btnTouchstart');
			},
			touchmove(e) {
				// 单指触摸
				if (e.touches.length !== 1) {
					return false;
				}
				
				this.isMove = true;
				
				this.left = e.touches[0].clientX - this.offsetWidth;
				
				let clientY = e.touches[0].clientY - this.offsetHeight;
				// #ifdef H5
					clientY += this.height;
				// #endif
				let edgeBottom = this.windowHeight - this.height - this.edge;

				// 上下触及边界
				if (clientY < this.edge) {
					this.top = this.edge;
				} else if (clientY > edgeBottom) {
					this.top = edgeBottom;
				} else {
					this.top = clientY
				}
				
			},
			touchend(e) {
				if (this.isDock) {
					let edgeRigth = this.windowWidth - this.width - this.edge;
					
					if (this.left < this.windowWidth / 2 - this.offsetWidth) {
						this.left = this.edge;
					} else {
						this.left = edgeRigth;
					}
					
				}
				
				this.isMove = false;
				
				this.$emit('btnTouchend');
			},
			kaiguan(){
				this.$emit('kaiguanClick');
			},
			gnkaiguan(id,kaiguan){
				this.$emit('gnkaiguan',id,kaiguan);
				this.$emit('kaiguanClick');

			}
		}}
</script>

<style lang="scss">
	.drag {
		display: flex;
		/*justify-content: center;
		align-items: center;*/
		background-color: rgba(98, 98, 98, 0.5);
		box-shadow: 0 0 6upx rgba(127, 127, 127, 0.5);
		color: $uni-text-color-inverse;
		width: 160upx;
		height: 600upx;
		border-radius: 30rpx;
		font-size: $uni-font-size-sm;
		position: fixed;
		z-index: 999999;
		
		&.transition {
			transition: left .3s ease,top .3s ease;
		}
	}

	
</style>
